package com.chen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.domain.SysRole;
import com.chen.dto.R;
import com.chen.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

/**
 * @Author: chen
 * @Date: 2021/4/20 2:11 PM
 * @Version 1.0
 */
@RestController
@Api(tags = "角色相关的控制器")
@RequestMapping("/roles")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    //按条件分页查询角色列表 	GET/roles
    @GetMapping
    @ApiOperation(value = "按条件分页查询角色列表 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数"),
            @ApiImplicitParam(name = "name", value = "角色名称"),
            @ApiImplicitParam(name = "size", value = "每量")

    })
    @PreAuthorize("hasAuthority('sys_role_query')")
    public R<Page<SysRole>> list(@ApiIgnore Page<SysRole> page,
                                 String name) {
        Page<SysRole> pages = sysRoleService.pageList(page, name);
        return R.ok(pages);
    }


    //新建角色    POST/roles
    @PostMapping
    @ApiOperation(value = "新建角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称"),
            @ApiImplicitParam(name = "code", value = "角色的代码")

    })
    @PreAuthorize("hasAuthority('sys_role_create')")
    public R insert(@RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return R.ok();
    }

    //删除角色	POST/roles/delete
    @PostMapping("/delete")
    @ApiOperation(value = "删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "角色id数组"),
    })
    @PreAuthorize("hasAuthority('sys_role_delete')")
    public R delete(@RequestBody String[] ids) {
        sysRoleService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


    //角色信息	GET/roles/{roleId}

}
